/*
 * Copyright (c) 2006-2018, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2018-11-06     SummerGift   first version
 * 2018-11-19     flybreak     add stm32f407-atk-explorer bsp
 */

#include <stdio.h>
#include <stdlib.h>
#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
#include <fal.h>
#include "setting.h" 
#include <rtdbg.h>

#include <dfs_fs.h>
#include <dfs_romfs.h>
#include "flashtochip.h"

#define LED0_PIN    GET_PIN(E, 15)    /* defined the LED0 pin: PF9 */

static rt_thread_t tid1 = RT_NULL;

int LED_TASK_INIT(void);
void Vled_task(void *parameter);


/****************************************************************************/
int main(void)
{

  rt_pin_mode(LED0_PIN, PIN_MODE_OUTPUT);
	fal_init();
	if (dfs_mount(RT_NULL, "/", "rom", 0, &(romfs_root)) == 0)
		LOG_I("ROM file System init successed!");
	else
		LOG_I("ROM file System init failed!");

	if(rt_device_find("W25Q128") != RT_NULL) 
	 {	
			if(dfs_mount("W25Q128", "/spiflash", "elm", 0, 0) == 0)		
				 rt_kprintf("spi flash mount to /spiflash \n");
			else 
				{
					dfs_mkfs("elm", "W25Q128");
					if(dfs_mount("W25Q128", "/spiflash", "elm", 0, 0)==0)
					{rt_kprintf("spi flash mount to /spiflash \n");
					}
				}
	 }
	OTA_Check();	
	while (1)
	 {
			rt_pin_write(LED0_PIN, PIN_HIGH);
			rt_thread_mdelay(100);
			rt_pin_write(LED0_PIN, PIN_LOW);
			rt_thread_mdelay(3000);
	 }


return 0;		
}
/****************************************************************************/

/****************************************************************************/	 
int LED_TASK_INIT(void)
{
	tid1=rt_thread_create("led",Vled_task,RT_NULL,256,12,500);
	if(tid1!=RT_NULL)
		{ rt_thread_startup(tid1);
			tid1=RT_NULL;
		}
}
/****************************************************************************/

/****************************************************************************/	 
static void Vled_task(void *parameter)
{
  int count = 1;

	/* set LED0 pin mode to output */
	rt_pin_mode(LED0_PIN, PIN_MODE_OUTPUT);
	while (count++)
	{
			rt_pin_write(LED0_PIN, PIN_HIGH);		
			rt_thread_mdelay(200);
			rt_pin_write(LED0_PIN, PIN_LOW);
			rt_thread_mdelay(2000);

	}
}
/**************************** end *****************************************/

